package com.ls.markt.controller.admin;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ls.markt.core.utils.DateUtils;
import com.ls.markt.model.admin.Coach;
import com.ls.markt.service.admin.CoachService;
import com.ls.markt.vo.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.text.ParseException;
import java.util.List;

/**
 * 教练管理控制器类
 */
@Controller
@RequestMapping("/coach")
public class CoachController {

    @Autowired
    private CoachService coachService;

    @RequestMapping("/index")
    public String index(){
        return "admin/coach/index";
    }

    /**
     * 查询全部教练
     * @return
     */
    @RequestMapping("/list")
    @ResponseBody
    public JSONObject list(@RequestParam(value="page",required=false,defaultValue="1")int page,
                                   @RequestParam(value="rows",required=false,defaultValue="10")int rows) throws ParseException {
        PageRequest pageRequest = PageRequest.of(page-1,rows);
        Page<Coach> coaches = coachService.listCoachs(pageRequest);
        JSONArray jsonArray = new JSONArray();
        if (coaches.getSize()>0){
            for (Coach coach:coaches){
                JSONObject playerJson = (JSONObject)JSONObject.toJSON(coach);
                playerJson.put("age", DateUtils.format(coach.getBirthday(),"yyyy-MM-dd"));
                jsonArray.add(playerJson);
            }
        }
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("total",coaches.getTotalElements());
        jsonObject.put("rows",jsonArray);
        return jsonObject;
    }

    @RequestMapping("/getCoachTree")
    @ResponseBody
    public JSONArray getCoachTree(){
        JSONArray jsonTree = new JSONArray();
        List<Coach> coaches = coachService.listCoachs();
        for (Coach coach:coaches){
            JSONObject jNode = new JSONObject();
            jNode.put("id",coach.getId());
            jNode.put("text",coach.getName());
            jNode.put("value",coach.getId());
            jsonTree.add(jNode);
        }
        return jsonTree;
    }

    /**
     * 跳转到新增页面
     * @return
     */
    @GetMapping("/add")
    public String add(){
        return "admin/coach/add";
    }

    /**
     * 跳转到编辑页面
     * @param id
     * @param model
     * @return
     */
    @GetMapping("/edit/{id}")
    public ModelAndView edit(@PathVariable("id")Long id, Model model){
        Coach coach = coachService.getCoachById(id);
        model.addAttribute("coach",coach);
        return new  ModelAndView("admin/coach/edit","coachModel",model);
    }

    /**
     * 执行修改教练
     * @param coach
     * @return
     */
    @PostMapping("/save")
    @ResponseBody
    public Response save(Coach coach){
        coachService.saveCoach(coach);
        return new Response(true);
    }

    /**
     * 执行删除球员
     * @param ids
     * @return
     */
    @PostMapping("/doDelete")
    @ResponseBody
    public Response doDelete(@RequestParam Long[] ids){
        coachService.removeCoach(ids);
        return new Response(true);
    }
}
